# -*- coding: utf-8 -*-
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TuringClass.settings')
import django
from app_TuringClass.models import *
from app_accounts.models import *
from django.contrib.auth.models import User
import datetime
## author：houchaoqun
## function：初始化用户数据库
## date：2016/11/10

def Population():
	### UserID, PassWord, RoleTypeID, NickName
	AddNewUsers('student_hcq', '123456', 'student', 'student_hcq')
	AddNewUsers('hcq001', '123456', 'admin', 'admin_hcq')
	AddNewUsers('TuringClass', '123456', 'student', '图灵系统编辑者')
	# AddNewUsers('Volunteer1', '123456', 'volunteer', '志愿者1')
	# AddNewUsers('Volunteer2', '123456', 'volunteer', '志愿者2')
	# AddNewUsers('Volunteer3', '123456', 'volunteer', '志愿者3')
	# AddNewUsers('Volunteer4', '123456', 'volunteer', '志愿者4')
	# AddNewUsers('Volunteer5', '123456', 'volunteer', '志愿者5')
	# AddNewUsers('Volunteer6', '123456', 'volunteer', '志愿者6')
	# AddNewUsers('Volunteer7', '123456', 'volunteer', '志愿者7')

	AddNewUsers('Master', '123456', 'admin', '信科计算机系硕')
	AddNewUsers('Undergraduate', '123456', 'admin', '信科本科生')


	### RoleTypeID, RoleTypeName, DeletePermission, PostPermission, SuperPermission
	AddNewRoleType('admin', '管理员', True, True, True)
	AddNewRoleType('student', '学生用户', True, True, False)
	AddNewRoleType('volunteer', '志愿者', False, False, False)


def AddNewUsers(UserID, PassWord, RoleTypeID, NickName):
	print "[UserID] = ",UserID
	if TuringUser.objects.filter(UserID = UserID).exists():
		print "[User has been existed...]"
	else:
		NewUser = User.objects.create_user(username = UserID, password = PassWord)
		NewTuringUser = TuringUser.objects.get_or_create(CurrentTime=timezone.now(),LastLoginTime=timezone.now(),Users = NewUser, UserID = UserID, RoleTypeID = RoleTypeID, NickName = NickName)[0]
		NewTuringUser.save()
		print "-- function [AddNewUsers] successful... "

def AddNewRoleType(RoleTypeID, RoleTypeName, DeletePermission, PostPermission, SuperPermission):
	if TuringRoleType.objects.filter(RoleTypeID = RoleTypeID).exists():
		print "[RoleTypeID] = ",RoleTypeID + ", has been existed!"
	else:
		print "Table [TuringRoleType] RoleTypeID = ", RoleTypeID
		_TuringUser = TuringRoleType.objects.get_or_create(RoleTypeID = RoleTypeID, RoleTypeName = RoleTypeName, DeletePermission = DeletePermission, PostPermission = PostPermission, SuperPermission = SuperPermission)[0]
		_TuringUser.save()


### main program
if __name__ == '__main__':
	print 'DatabaseInit.py - running...'
	Population()
